Passed
Push — development ( 989440...b94626 )
by Karl
25:06 queued 11:57
created

TravelController.startRentingBike   A

Complexity

Conditions 1

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 21
dl 0
loc 27
ccs 3
cts 3
cp 1
crap 1
rs 9.376
c 0
b 0
f 0
1 8
import {
0 ignored issues
show
introduced by
Replace ...seGuards,⏎··Req,⏎ with ·Controller,·Get,·Par...ody,·UseGuards,·Req·
Loading history...
2
  Controller,
3
  Get,
4
  Param,
5
  Post,
6
  Body,
0 ignored issues
show
introduced by
'Body' is defined but never used.
Loading history...
7
  UseGuards,
8
  Req,
9
} from '@nestjs/common';
10 8
import {
0 ignored issues
show
introduced by
Replace ...aram,⏎··ApiTags,⏎ with ·ApiOperation,·ApiResp...,·ApiParam,·ApiTags·
Loading history...
11
  ApiOperation,
12
  ApiResponse,
13
  ApiBearerAuth,
14
  ApiParam,
15
  ApiTags,
16
} from '@nestjs/swagger';
17 8
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
18 8
import { TravelService } from './travel.service';
19 8
import {
0 ignored issues
show
introduced by
Replace ...⏎··EndTravelDto,⏎ with ·StartRentingDto,·Trav...nseDto,·EndTravelDto·
Loading history...
20
  StartRentingDto,
0 ignored issues
show
introduced by
'StartRentingDto' is defined but never used.
Loading history...
21
  TravelResponseDto,
22
  EndTravelDto,
0 ignored issues
show
introduced by
'EndTravelDto' is defined but never used.
Loading history...
23
} from './dto/renting.dto';
24
25
@ApiTags('Bike Rentals')
26
@Controller({ path: 'rental', version: '1' })
27 8
export class TravelController {
28 7
  constructor(private readonly travelService: TravelService) {}
29
30
  @Get('customer/:customerId')
31
  // @UseGuards(JwtAuthGuard)
32
  // @ApiBearerAuth()
33
  @ApiOperation({
34
    summary: 'Get all travels for a customer',
35
    description: 'Returns all travels for a specific customer',
36
  })
37
  @ApiResponse({
38
    status: 200,
39
    description: 'Travels found',
40
    type: [TravelResponseDto],
41
  })
42
  @ApiResponse({
43
    status: 404,
44
    description: 'No travels found for this customer',
45
  })
46 8
  async getTravelsForCustomer(@Param('customerId') customerId: string) {
47 1
    return await this.travelService.findTravelsForCustomer(customerId);
48
  }
49
50
  @Get('bike/:bikeId/active')
51
  @ApiOperation({
52
    summary: 'Get active travel for a bike',
53
    description:
54
      'Returns the active travel information including renter details for a specific bike',
55
  })
56
  @ApiResponse({
57
    status: 200,
58
    description: 'Active travel found',
59
    type: TravelResponseDto,
60
  })
61
  @ApiResponse({
62
    status: 404,
63
    description: 'No active travel found for this bike',
64
  })
65 8
  async getActiveTravelForBike(@Param('bikeId') bikeId: string) {
66 1
    return await this.travelService.findActiveTravelForBike(bikeId);
67
  }
68
69
  @Post('bike/:bikeId/end-active')
70
  @ApiOperation({
71
    summary: 'End active travel for specific bike',
72
    description: 'Ends the currently active travel for the specified bike',
73
  })
74
  @ApiResponse({
75
    status: 201,
76
    description: 'Travel ended successfully',
77
  })
78
  @ApiResponse({
79
    status: 404,
80
    description: 'No active travel found for this bike',
81
  })
82 8
  async endActiveBikeTravel(@Param('bikeId') bikeId: string) {
83
    return await this.travelService.endActiveTravelForBike(bikeId);
84
  }
85
  
0 ignored issues
show
introduced by
Delete ··
Loading history...
86
  @Post(':githubId/end-all-travels')
87
  @UseGuards(JwtAuthGuard)
88
  @ApiBearerAuth()
89
  @ApiOperation({
90
    summary: 'End all active travels for a customer',
91
    description: 'Ends all active travels for the specified customer by GitHub ID.',
92
  })
93
  @ApiParam({
94
    name: 'githubId',
95
    description: 'The GitHub ID of the user whose travels are to be ended.',
96
    example: '12345',
97
  })
98
  @ApiResponse({
99
    status: 200,
100
    description: 'All active travels for the customer have been ended.',
101
  })
102
  @ApiResponse({
103
    status: 404,
104
    description: 'Customer or active travels not found.',
105
  })
106 8
  async endAllTravelsForCustomer(@Param('githubId') githubId: string) {
107
    return await this.travelService.endAllTravelsForCustomer(githubId);
108
  }
0 ignored issues
show
introduced by
Delete
Loading history...
109
110
111
  // Start a bike rental
112
  @Post('bike/:id')
113
  @UseGuards(JwtAuthGuard)
114
  @ApiBearerAuth()
115
  @ApiOperation({
116
    summary: 'Start renting a bike',
117
    description:
118
      'Initiates a bike travel for the authenticated user. The bike must be available for travel.',
119
  })
120
  @ApiResponse({
121
    status: 201,
122
    description: 'Bike travel started successfully',
123
    type: TravelResponseDto,
124
  })
125
  @ApiResponse({
126
    status: 400,
127
    description: 'Bad Request - Bike not found or not available for renting',
128
  })
129
  @ApiResponse({
130
    status: 401,
131
    description: 'Unauthorized - User not authenticated',
132
  })
133 8
  async startRentingBike(@Param('id') id: string, @Req() req: any) {
134 1
    const userId = req.user.githubId;
135 1
    return this.travelService.startRentingBike(id, userId);
136
  }
137
138
  // End a bike travel
139
  @Post(':id/end')
140
  // removing the guard for now, the bike software should be able to end a travel.
141
  // we could implement a guard that authenticates the bike somehow
142
  // we could also expand on this, making it even more complex, like checking that it is the user whom initlized the rental that is ending it
143
  // @UseGuards(JwtAuthGuard)
144
  @ApiOperation({
145
    summary: 'End a bike travel',
146
    description:
0 ignored issues
show
introduced by
Delete ⏎·····
Loading history...
147
      'Ends the travel, calculates cost, and makes the bike available again',
148
  })
149
  @ApiResponse({
150
    status: 201,
151
    description: 'Travel ended successfully',
152
  })
153
  @ApiResponse({
154
    status: 400,
155
    description: 'Bad Request - Travel not found or already ended',
156
  })
157
  @ApiResponse({
158
    status: 401,
159
    description: 'Unauthorized',
160
  })
161 8
  async endTravel(@Param('id') travelId: number) {
162 1
    return this.travelService.endTravel(travelId);
163
  }
164
165
  // Fetch one travel by ID
166
  @Get(':id')
167 8
  async getTravelById(@Param('id') id: number) {
168 1
    return await this.travelService.findById(id);
169
  }
170
171
  // Fetch all travels
172
  @Get()
173 8
  async getAllTravels() {
174 1
    return await this.travelService.findAll();
175
  }
176
}
177